package com.zsy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zsy.mapper.StudentMapper;
import com.zsy.pojo.PageResult;
import com.zsy.pojo.Student;
import com.zsy.pojo.StudentCountOption;
import com.zsy.pojo.StudentQueryParam;
import com.zsy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-09-06 21:14
 **/
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 分页查询
     *
     * @param studentQueryParam
     * @return
     */
    @Override
    public PageResult<Student> queryStudent(StudentQueryParam studentQueryParam) {
        // 1.获取分页参数
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        // 2.执行查询
        List<Student> students = studentMapper.queryStudent(studentQueryParam);
        // 3.封装数据
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 增加学生
     *
     * @param student
     */
    @Override
    public void addStudent(Student student) {
        // 处理辅助字段
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.addStudent(student);
    }

    /**
     * 根据id查询学生
     *
     * @param id
     * @return
     */
    @Override
    public Student queryStudentById(Integer id) {
        return studentMapper.queryStudentById(id);
    }

    /**
     * 修改学生
     *
     * @param student
     */
    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    /**
     * 批量删除学生
     *
     * @param ids
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    /**
     * @param id    学生id
     * @param score 学生违纪分数
     */
    @Override
    public void updateViolationStu(Integer id, int score) {
        Student student = studentMapper.queryStudentById(id);
        Short violationCount = student.getViolationCount();
        int violationScore = student.getViolationScore();
        if (violationCount == 0){
            violationCount = 1;
        }
        else {
            violationCount++;
        }
        if (violationScore == 0){
            violationScore= score;
        }
        else {
            violationScore = violationScore + score ;
        }
        LocalDateTime updateTime = LocalDateTime.now();
        studentMapper.updateViolationStu(id,violationCount,violationScore,updateTime);
    }

    /**
     * 统计各个班级的学生人数
     */
    @Override
    public StudentCountOption getStudentCountData() {
       List<Map> mapList = studentMapper.getStudentCountData();
       List clazzList = new ArrayList();
       List totalList = new ArrayList();
        for (Map map : mapList) {
            clazzList.add(map.get("name"));
            totalList.add(map.get("value"));
        }
        return new StudentCountOption(clazzList,totalList);
    }

    /**
     * 统计各个学历的学生人数
     *
     * @return
     */
    @Override
    public List<Map> getStudentDegreeData() {
        List<Map> mapList = studentMapper.getStudentDegreeData();
        return mapList;
    }
}
